<script>
    import { mapActions, mapMutations } from 'vuex'
    import config from '@/admin.config.js'
    import { version } from './package.json'
    import { uniAdminCacheKey } from './common/store/constants.js'
    import jsonBig from 'json-bigint'

    export default {
        created() {
            this.clear = undefined
        },
        onLaunch: function() {
            console.log(
                `%c yue-open-devops-deploy %c v${version} `,
                'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
                'background:#007aff ;padding: 1px; border-radius: 0 3px 3px 0;  color: #fff; font-weight: bold;'
            )
            console.log('App Launch')

            // theme
            this.SET_THEME(uni.getStorageSync(uniAdminCacheKey.theme) || 'default')
            
            // http拦截器
            this.httpInterceptor()
        },
        onShow: function() {
            console.log('App Show')
            this.clear = setInterval(() => this.clearPlatform(), 15 * 60 * 1000)
        },
        onHide: function() {
            console.log('App Hide')
            this.clear && clearInterval(this.clear)
        },
        onPageNotFound(msg) {
            uni.redirectTo({
                url: config.error.url
            })
        },
        methods: {
            ...mapMutations('app', ['SET_THEME']),
            ...mapActions({
                init: 'app/init'
            }),
            clearPlatform() {
                const keysOfPlatform = uni.getStorageInfoSync().keys.filter(key => key.indexOf('platform') > -1)
                keysOfPlatform.length && keysOfPlatform.forEach(key => uni.removeStorageSync(key))
            },
			// HTTP拦截器：url、param、token等
			httpInterceptor() {
                let this_ = this;
				uni.addInterceptor('request', {
					invoke(args) {
						if (args.url.startsWith("http")) {
							return;
						}
						
						// 开发环境拼接host
						if (import.meta.env.DEV) {
							args.url = 'http://' + window.location.hostname + ':9999' + args.url;
						}
						
						uni.showLoading({
							title: '加载中'
						});
					},
					success(resp) {
						let data = resp.data;
						if ((typeof data) == 'string') {
							data = jsonBig.parse(data);
                            resp.data = data;
						}
						console.log('interceptor-success', resp)
						uni.hideLoading();
						var code = data.code;
						if (code != 200) {
                            if (code == 600) {
                                uni.showModal({
                                    title: "操作错误",
                                    content: data.msg,
                                    showCancel: false
                                })
                            } else {
                                uni.showToast({
                                    title: data.msg,
                                    icon: 'error',
                                    duration: 2000
                                });
                            }
							return false;
						}
					},
					fail(err) {
						console.log('interceptor-fail', err)
						uni.hideLoading();
						uni.showToast({
							title: "未知错误请重试",
							icon: 'error',
							duration: 3000
						});
					},
					returnValue(promise) {
						if (promise instanceof Promise) {
							promise.then((res) => {
								let data = res.data;
								if ((typeof data) == 'string') {
									data = jsonBig.parse(data);
								}
								res.data = data
							})
						}
					}
				})
			}
        }
    }
</script>

<style lang="scss">
    @import '@/common/style/uni.css';
    @import '@/common/style/uni-icons.css';
    @import '@/common/style/admin-icons.css';
    @import '@/common/style/theme.scss';
</style>
